// pages/index/index.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: config.basePath,
    current: 0, //当前所在页面的 index
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔
    duration: 800, //滑动动画时长
    circular: false, //是否采用衔接滑动
    swiperCurrent: 0,
    indicatorColor: "#00000033",
    indicatorActiveColor: "white",
    swiperList: [],
    fullLoading: true,
    articleLoding: true,
    articleData: [],
    total: 0,
    pageIndex: 1,
    showCopyLink: false,
    link: '',
    hotArticleData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: config.miniName
    });
    this.getSetting();
    this.getArticleList();
    this.getHotArticle();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      fullLoading: false
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      pageIndex: 1,
      total: 0,
      articleData: []
    });
    that.getArticleList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.total <= that.data.articleData.length) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1
    });
    that.getArticleList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onShareTimeline(res) {
    return {
      title: config.miniName,
      path: '/pages/index/index'
    }
  },

  /**
   * 轮播切换
   */
  swiperChange: function (e) {
    if (this.data.current == 0 && this.data.swiperCurrent > 1) { //卡死时，重置current为正确索引
      this.setData({
        current: this.data.swiperCurrent
      });
    } else { //正常轮转时，记录正确页码索引
      this.setData({
        swiperCurrent: e.detail.current
      });
    }
  },
  animationChange(e) {
    this.setData({
      swiperCurrent: e.detail.current
    });
  },

  /**
   * 点击轮播图片触发事件
   */
  swipclick: function (e) {
    let that = this;
    if (that.data.swiperList[that.data.swiperCurrent].link.indexOf("/pages") == 0) {
      wx.navigateTo({
        url: that.data.swiperList[that.data.swiperCurrent].link
      })
    } else {
      that.setData({
        link: that.data.swiperList[that.data.swiperCurrent].link,
        showCopyLink: true
      });
    }
  },

  goArticle: function (event) {
    wx.navigateTo({
      url: "/pages/article/article?id=" + event.currentTarget.dataset.id
    })
  },

  getArticleList: function () {
    let that = this;
    that.setData({
      articleLoging: true
    });
    wx.request({
      url: config.basePath + '/mini/article/list',
      method: 'GET',
      data: {
        pageIndex: that.data.pageIndex,
        pageSize: 6
      },
      success(res) {
        if (res.data.code === 200) {
       let articleData=  res.data.data.map(function(item){
         let data=item
          let s= config.basePath+ item.thumbnail
         console.log(s)
         let b=item.thumbnail
         if(!(item.thumbnail.slice(0,4)=='http')){
           s= config.basePath+ item.thumbnail
         }else{
          s=  item.thumbnail
          console.log('s1',item)
         }
         if(s==config.basePath){
           console.log('随机图片')
           s='https://api.vvhan.com/api/bing?rand=sj'
         }
         data.thumbnail=s
         return data
         })
        console.log(articleData)
          that.setData({
            articleData: that.data.articleData.concat(articleData),
            total: res.data.total,
            articleLoding: true
          });

          if (that.data.total <= that.data.articleData.length) {
            that.setData({
              articleLoding: false
            });
          }
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        that.setData({
          articleLoding: false
        });
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },

  getHotArticle: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/article/getHotListByView',
      method: 'GET',
      data: {
        pageIndex: 1,
        pageSize: 4
      },
      success(res) {
        if (res.data.code === 200) {
          let articleData=  res.data.data.map(function(item){
            let data=item
             let s= config.basePath+ item.thumbnail
            console.log(s)
            let b=item.thumbnail
            if(!(item.thumbnail.slice(0,4)=='http')){
              s= config.basePath+ item.thumbnail
            }else{
             s=  item.thumbnail
             console.log('s1',item)
            }
            if(s==config.basePath){
              console.log('随机图片')
              s='https://api.vvhan.com/api/bing?rand=sj'
            }
            data.thumbnail=s
            return data
            })
          that.setData({
            hotArticleData: that.data.hotArticleData.concat(articleData),
          });
        } else {
          wx.lin.showMessage({
            type: 'error',
            content: '服务出错'
          })
        }
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },
  getSetting: function () {
    let that = this;
    wx.request({
      url: config.basePath + '/mini/system/getSetting',
      method: 'GET',
      success(res) {
        if (res.data.code === 200) {
          if (res.data.data.swiper != "" && res.data.data.swiper.replace(/(^s*)|(s*$)/g, "").length > 0) {
            that.setData({
              swiperList: JSON.parse(res.data.data.swiper)
            })
          }
        }
      },
      fail(err) {
        wx.lin.showMessage({
          type: 'error',
          content: '网络出错'
        })
      }
    });
  },
  copyLink: function () {
    let that = this;
    wx.setClipboardData({
      data: that.data.link,
      success: function (res) {}
    })
  }
})